<template>
   <view class="container">
      <uv-textarea v-model="value" placeholder="请输入您的url" maxlength="-1"></uv-textarea>
      <uv-button type="primary" text="获取token" @click="getTokenBtn"></uv-button>
      <view class="container__bbh">当前版本：1.0.5</view>
   </view>
</template>

<script>
   import { Refresh, get_tokem, get_code } from "@/api/http/index.js";
   import { addUser } from "@/api/http/myNodeIndex.js";
   const { decodeJwt } = require("jose");
   export default {
      data() {
         return {
            value: "",
         };
      },
      methods: {
         getTokenBtn() {
            const regex = /skey=([^&]+)/;
            const match = this.value.match(regex);
            if (match) {
               const skey = match[1];
               this.getToken(skey);
            }
         },
         // 换取token
         getToken(skeyStr) {
            let data = {
               merchantNo: "E888888886",
               appId: "f780ea5b76544c1fa20da8048f1dd424",
               skey: skeyStr,
            };
            get_tokem(data).then((res) => {
               const decodedToken = decodeJwt(res.token);
               let myNodeData = {
                  token: res.token,
                  teamName: res.teamName,
                  expirationTime: decodedToken.exp,
                  uid: res.uid,
                  merchantNo: res.data.merchantNo,
               };
			   
               if (res.code == 0) {
                  addUser(myNodeData).then((res) => {
                     if (res.code == 1) {
                        uni.navigateBack({
                           delta: 1,
                           success: () => {
                              vk.toast("添加成功！", "none");
                           },
                        });
                     } else {
                        vk.toast(res.msg, "none");
                     }
                  });
               } else {
                  vk.toast(res.msg, "none");
               }
            });
         },
      },
   };
</script>

<style scoped lang="scss">
   @import '../../uni.scss';
   
   .container {
      display: flex;
      flex-direction: column;
      padding: 30rpx;
      
      &__bbh {
         margin-top: 20rpx;
         text-align: center;
         font-size: 24rpx;
         color: #888;
      }
      
      :deep(.uv-button--primary) {
         background-color: $uni-theme-color !important;
         margin-top: 30rpx;
      }
   }
</style>
